//
//  InputServerDetailPage.swift
//  FlameB
//
//  Created by 连俊 on 2020/8/5.
//  Copyright © 2020 连俊. All rights reserved.
//

import UIKit

class InputServerDetailPage: BaseViewController {
    var timeTextfield: UITextField!
    var yearTextfield: UITextField!
    var monthTextfield: UITextField!
    var dayTextfield: UITextField!
    
    var inputButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "100m"
    }
    
    override func buildView() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewDidEdit))
        view.addGestureRecognizer(tap)
                
        let view1 = UIView()
        view1.backgroundColor = UIColor.white
        view1.layer.borderColor = UIColor.black.cgColor
        view1.layer.borderWidth = 1/UIScreen.main.scale
        view1.layer.cornerRadius = 5
        view1.layer.masksToBounds = true
        view.addSubview(view1)
        view1.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(GTSize_width(200))
            make.left.equalTo(self.view.snp.left).offset(GTSize_width(20))
            make.width.equalTo(160)
            make.height.equalTo(GTSize_width(17+28))
        }
        
        timeTextfield = UITextField()
        let placeholderStr = "请输入秒数"
        let attributedString = NSMutableAttributedString(string: placeholderStr)
        attributedString.addAttributes([
            NSAttributedString.Key.font:UIFont.mainFont(ofSize: GTSize_width(17)),
            NSAttributedString.Key.foregroundColor:UIColor.init(rgb: 0xBBBCBB)
            ], range: NSRange(location: 0, length: placeholderStr.count))
        timeTextfield.attributedPlaceholder = attributedString
        timeTextfield.font = UIFont.mainFont(ofSize: GTSize_width(17))
        timeTextfield.textColor = UIColor.black
        timeTextfield.tag = 100
        timeTextfield.clearButtonMode = .whileEditing
        timeTextfield.addTarget(self, action: #selector(editChange(_ :)), for: .editingChanged)
        timeTextfield.keyboardType = .phonePad
        view1.addSubview(timeTextfield)
        timeTextfield.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(GTSize_width(4))
            make.bottom.equalToSuperview().offset(GTSize_width(-4))
            make.left.equalToSuperview().offset(GTSize_width(8))
            make.right.equalToSuperview().offset(GTSize_width(-4))
        }
        
        let view2 = UIView()
        view2.backgroundColor = UIColor.white
        view2.layer.borderColor = UIColor.black.cgColor
        view2.layer.borderWidth = 1/UIScreen.main.scale
        view2.layer.cornerRadius = 5
        view2.layer.masksToBounds = true
        view.addSubview(view2)
        view2.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(GTSize_width(20))
            make.left.equalTo(self.view.snp.left).offset(GTSize_width(20))
            make.width.equalTo(100)
            make.height.equalTo(GTSize_width(17+28))
        }
        
        yearTextfield = UITextField()
        let placeholderStr1 = "请输入年"
        let attributedString1 = NSMutableAttributedString(string: placeholderStr1)
        attributedString1.addAttributes([
            NSAttributedString.Key.font:UIFont.mainFont(ofSize: GTSize_width(17)),
            NSAttributedString.Key.foregroundColor:UIColor.init(rgb: 0xBBBCBB)
            ], range: NSRange(location: 0, length: placeholderStr1.count))
        yearTextfield.attributedPlaceholder = attributedString1
        yearTextfield.font = UIFont.mainFont(ofSize: GTSize_width(17))
        yearTextfield.textColor = UIColor.black
        yearTextfield.tag = 101
        yearTextfield.clearButtonMode = .whileEditing
        yearTextfield.addTarget(self, action: #selector(editChange(_ :)), for: .editingChanged)
        yearTextfield.keyboardType = .phonePad
        view2.addSubview(yearTextfield)
        yearTextfield.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(GTSize_width(4))
            make.bottom.equalToSuperview().offset(GTSize_width(-4))
            make.left.equalToSuperview().offset(GTSize_width(8))
            make.right.equalToSuperview().offset(GTSize_width(-4))
        }
        
        let view3 = UIView()
        view3.backgroundColor = UIColor.white
        view3.layer.borderColor = UIColor.black.cgColor
        view3.layer.borderWidth = 1/UIScreen.main.scale
        view3.layer.cornerRadius = 5
        view3.layer.masksToBounds = true
        view.addSubview(view3)
        view3.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(GTSize_width(20))
            make.left.equalTo(view2.snp.right).offset(GTSize_width(20))
            make.width.equalTo(100)
            make.height.equalTo(GTSize_width(17+28))
        }
        
        monthTextfield = UITextField()
        let placeholderStr2 = "请输入月"
        let attributedString2 = NSMutableAttributedString(string: placeholderStr2)
        attributedString2.addAttributes([
            NSAttributedString.Key.font:UIFont.mainFont(ofSize: GTSize_width(17)),
            NSAttributedString.Key.foregroundColor:UIColor.init(rgb: 0xBBBCBB)
            ], range: NSRange(location: 0, length: placeholderStr2.count))
        monthTextfield.attributedPlaceholder = attributedString2
        monthTextfield.font = UIFont.mainFont(ofSize: GTSize_width(17))
        monthTextfield.textColor = UIColor.black
        monthTextfield.tag = 102
        monthTextfield.clearButtonMode = .whileEditing
        monthTextfield.addTarget(self, action: #selector(editChange(_ :)), for: .editingChanged)
        monthTextfield.keyboardType = .phonePad
        view3.addSubview(monthTextfield)
        monthTextfield.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(GTSize_width(4))
            make.bottom.equalToSuperview().offset(GTSize_width(-4))
            make.left.equalToSuperview().offset(GTSize_width(8))
            make.right.equalToSuperview().offset(GTSize_width(-4))
        }
        
        let view4 = UIView()
        view4.backgroundColor = UIColor.white
        view4.layer.borderColor = UIColor.black.cgColor
        view4.layer.borderWidth = 1/UIScreen.main.scale
        view4.layer.cornerRadius = 5
        view4.layer.masksToBounds = true
        view.addSubview(view4)
        view4.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(GTSize_width(20))
            make.left.equalTo(view3.snp.right).offset(GTSize_width(20))
            make.width.equalTo(100)
            make.height.equalTo(GTSize_width(17+28))
        }
        
        dayTextfield = UITextField()
        let placeholderStr3 = "请输入日"
        let attributedString3 = NSMutableAttributedString(string: placeholderStr3)
        attributedString3.addAttributes([
            NSAttributedString.Key.font:UIFont.mainFont(ofSize: GTSize_width(17)),
            NSAttributedString.Key.foregroundColor:UIColor.init(rgb: 0xBBBCBB)
            ], range: NSRange(location: 0, length: placeholderStr3.count))
        dayTextfield.attributedPlaceholder = attributedString3
        dayTextfield.font = UIFont.mainFont(ofSize: GTSize_width(17))
        dayTextfield.textColor = UIColor.black
        dayTextfield.tag = 103
        dayTextfield.clearButtonMode = .whileEditing
        dayTextfield.addTarget(self, action: #selector(editChange(_ :)), for: .editingChanged)
        dayTextfield.keyboardType = .phonePad
        view4.addSubview(dayTextfield)
        dayTextfield.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(GTSize_width(4))
            make.bottom.equalToSuperview().offset(GTSize_width(-4))
            make.left.equalToSuperview().offset(GTSize_width(8))
            make.right.equalToSuperview().offset(GTSize_width(-4))
        }
        
        inputButton = UIButton()
        inputButton.layer.cornerRadius = 5
        inputButton.layer.masksToBounds = true
        inputButton.isEnabled = false
        inputButton.setTitle("录入", for: .normal)
        inputButton.setTitleColor(.white, for: .normal)
        inputButton.setBackgroundImage(UIImage.image(color: UIColor.init(rgb: 0xC29756)), for: .normal)
        inputButton.setTitleColor(.white, for: .disabled)
        inputButton.setBackgroundImage(UIImage.image(color: UIColor.init(rgb: 0xBFBFBF)), for: .disabled)
        inputButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: GTSize_width(17))
        inputButton.addTarget(self, action: #selector(inputButtonAction(_:)), for: .touchUpInside)
        view.addSubview(inputButton)
        inputButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp.centerX)
            make.top.equalTo(view4.snp.bottom).offset(GTSize_width(150))
            make.size.equalTo(CGSize(width: GTSize_width(325), height: GTSize_width(45)))
        }
    }

    @objc private func viewDidEdit() {
        if timeTextfield.isFirstResponder {
            timeTextfield.resignFirstResponder()
        }
        if yearTextfield.isFirstResponder {
            yearTextfield.resignFirstResponder()
        }
        if monthTextfield.isFirstResponder {
            timeTextfield.resignFirstResponder()
        }
        if dayTextfield.isFirstResponder {
            yearTextfield.resignFirstResponder()
        }
    }
    
    @objc func editChange(_ sender: UITextField) {
        if sender.tag == 100 {
            textFieldLimit(with: sender, limit: 3)
        }
        else if sender.tag == 101 {
            textFieldLimit(with: sender, limit: 4)
        }
        else if sender.tag == 102 {
            textFieldLimit(with: sender, limit: 2)
        }
        else if sender.tag == 103 {
            textFieldLimit(with: sender, limit: 2)
        }
        checkNextEnable()
    }
    
    fileprivate func textFieldLimit(with tf: UITextField, limit: Int)  {
        guard let text = tf.text else {return}
        guard text.count > limit else {return}
        tf.text = String(text.prefix(limit))
    }
    
    func checkNextEnable() {
        if (timeTextfield.text?.count == 0 || yearTextfield.text?.count != 4 || monthTextfield.text?.count == 0 || dayTextfield.text?.count == 0) {
            inputButton.isEnabled = false
            return
        }
        inputButton.isEnabled = true
        view.endEditing(true)
    }
    
    @objc func inputButtonAction(_ sender: UIButton) {
        guard let time = timeTextfield.text, let year = yearTextfield.text, let month = monthTextfield.text, let day = dayTextfield.text else { return }
        let model = StoreIMBrand()
        model.brandId = 0
        model.brandName = ""
        model.brandLogoUrl = ""
        model.unreadMsgCount = 0
        model.lastMsgTime = year+"年"+month+"月"+day+"日"
        model.lastMsg = ""
        model.totalTime = Int(time) ?? 0
        DBService.shared.addBrand(model)
        
        let alertView = UIAlertController(title: "提示", message: "添加成功", preferredStyle: UIAlertController.Style.alert)
        let action1 = UIAlertAction(title: "确定", style: UIAlertAction.Style.default, handler: {[weak self] alert in
            self?.timeTextfield.text = ""
            self?.yearTextfield.text = ""
            self?.monthTextfield.text = ""
            self?.dayTextfield.text = ""
        })
        alertView.addAction(action1)
        self.present(alertView, animated: true, completion: nil)
        return
    }
}
